package edos.prm.process;

import java.util.HashSet;

import edos.prm.task.Task;

/**
 * An {@code Environment} is a helper class that is used by executing processes
 * to map variable names to object values.
 */
public final class Environment {
	private Task task;
	private HashSet<Variable> env;

	Environment(Task task) {
		this.task = task;
		this.env = new HashSet<Variable>();
	}

	void add(Variable var) {
		env.add(var);
	}
	
	Variable getVariable(String name) {
		for (Variable var : env )
			if ( var.name.equals(name) )
				return var;
		return null;
	}
	
	/**
	 * A {@code Variable} is a place holder for object values in a process
	 * declaration.
	 */
	static final class Variable {

		private String name;
		private Object value;
		
		Variable(String name, Object value) {
			this.name = name;
			this.value = value;
		}
		
		Object getValue() {
			return value;
		}
		
		void setValue(Object value) {
			this.value = value;
		}
		
		String getName() {
			return name;
		}

		@Override
		public int hashCode() {
			return name.hashCode();
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			Variable other = (Variable) obj;
			if (name == null) {
				if (other.name != null)
					return false;
			} else if (!name.equals(other.name))
				return false;
			return true;
		}

	}

	public static Environment getEnvironmentFor(edos.prm.process.Process process) {
		// TODO Auto-generated method stub - parse the process description and
		// extract variable tokens.
		return null;
	}

	public Task getTask() {
		return task;
	}
}
